<?php
/**
 * 
 * 模板中数据提供器接口
 * @author Leo Ning 2012-03-20
 *
 */
interface IDataProvider {
	/**
	 * 提供数据的主要方法
	 *
	 * @param array $params        	
	 * @return CtsDataSet
	 */
	public function provide($params);
}
/**
 * smarty标签cts数据集
 *
 * @author LeoNing 2012-03-27
 * 
 */
class CtsDataSet {
	private $data;
	private $pagingAry = array ();
	public function __construct($data, $total = 0, $per = 0) {
		$this->data = $data;
		if ($per > 0) {
			$this->paging ( $total, $per );
		}
	}
	
	/**
	 * 取用于cts标签的数据
	 *
	 * @return array(array(...))
	 */
	public function datas($data = false) {
		if ($data == false) {
			return $this->data;
		}
		$this->data = $data;
	}
	/**
	 * 取用于ctv标签的数据
	 * 
	 * @return mixed
	 */
	public function ctvdata() {
		if (is_array ( $this->data ) || $this->data instanceof ResultSet) {
			return empty ( $this->data ) ? array () : $this->data [0];
		} else {
			return $this->data;
		}
	}
	/**
	 * 取结果中的分页信息
	 *
	 * @return array('url'=>'基础url','total'=>'记录总数','cur'=>'当前页面','per'=>'每页记录个数')
	 */
	public function paging($total = 0, $per = 0) {
		global $__url, $__cpage;
		if ($per == 0) {
			return $this->pagingAry;
		}
		$__cpage = $__cpage == null ? 1 : $__cpage;
		$url = explode ( '.', $__url );
		$ext = array_pop ( $url );
		$this->pagingAry = array ('url' => implode ( '.', $url ) . '_', 'cur' => $__cpage, 'total' => $total, 'per' => $per, 'ext' => '.' . $ext );
		return $this->pagingAry;
	}
}